package com.mallify.service.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.mallify.common.model.user.UserBill;
import com.mallify.common.request.FundsMonitorSearchRequest;
import com.mallify.common.request.PageParamRequest;
import com.mallify.service.mapper.UserBillMapper;
import com.mallify.service.service.UserBillService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * StoreOrderServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | HANSON [ HANSON赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Licensed HANSON并不是自由软件，未经许可不能去掉HANSON相关版权
 * +----------------------------------------------------------------------
 * | Author: HANSON Team <hanson0622@126.com>
 * +----------------------------------------------------------------------
 */
@Service
public class UserBillServiceImpl extends ServiceImpl<UserBillMapper, UserBill> implements UserBillService {

    @Resource
    private UserBillMapper userBillMapper;

    /**
     * 列表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<UserBill>
     */
    @Override
    public List<UserBill> getList(FundsMonitorSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<UserBill> lqw = new LambdaQueryWrapper<>();

        // 排序
        if (request.getSort() == null) {
            lqw.orderByDesc(UserBill::getCreateTime);
        } else {
            if ("asc".equals(request.getSort())) {
                lqw.orderByAsc(UserBill::getNumber);
            } else {
                lqw.orderByDesc(UserBill::getNumber);
            }
        }

        // 查询类型
        if (StringUtils.isNotBlank(request.getCategory())) {
            lqw.eq(UserBill::getCategory, request.getCategory());
        }
        if (ObjectUtil.isNotNull(request.getPm())) {
            lqw.eq(UserBill::getPm, request.getPm());
        }

        return userBillMapper.selectList(lqw);
    }
}